//
//  HomePageViewController.m
//  UserUse
//
//  Created by angra on 16/10/11.
//  Copyright © 2016年 CJPC. All rights reserved.
//

#import "HomePageViewController.h"
#import "LoginViewController.h"
#import "LLPersonMianViewController.h"
#import "HomeTableViewCell.h"
#import "SendDetailViewController.h"
#import "HomeOrderModel.h"

#import "LoginViewController.h"
#import "BaseNavigationController.h"
@interface HomePageViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UILabel * _zhanweiTitle;
}
@property (nonatomic,retain)UITableView *tableView;
@property (nonatomic,retain)NSMutableArray *dataArr;
@property (nonatomic,strong)UIView * zhanweiView;
@end


static NSString *const kHomeCellIdentifier = @"HomeTableViewCell";
@implementation HomePageViewController

- (UITableView *)tableView
{
    if(!_tableView)
    {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenFrameW, kScreenFrameH-64) style:UITableViewStylePlain];
        [self.view addSubview:self.tableView];
        _tableView.backgroundColor = kColoeString(@"f7f7f7");
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[HomeTableViewCell class] forCellReuseIdentifier:kHomeCellIdentifier];
        // 添加下拉,上拉刷新
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getData)];
        
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"拼车派单";
    
    [self setNavigation];
    [self getData];
    if (self.isDir) {
        [self setAutoLogin];
    }
    
}
-(void)setAutoLogin
{
 
    LLUserModel *model =  [LLUserHandle getUserData];
    [AllProjectRequest loginWithUserPhone:model.UserTelephone userpwd:model.passWord userType:@"0" success:^(id responseObject) {
        if (isSuccessRequest) {
            LLUserModel *user = [LLUserModel new];
            user.UserTelephone = model.UserTelephone ;
            user.passWord = model.passWord;
            user.UserName = responseObject[@"resultObject"][@"username"];
            user.userid = [NSString stringWithFormat:@"%@",responseObject[@"resultObject"][@"userid"]];
            user.UserIconUrl = responseObject[@"resultObject"][@"userheadpath"];
            user.usernumber = responseObject[@"resultObject"][@"usernumber"];
            [LLUserHandle saveUserDataWith:user];
    
        }
        else
        {
            LoginViewController *mainVC = [[LoginViewController alloc]init];
            BaseNavigationController *baseVC = [[BaseNavigationController alloc]initWithRootViewController:mainVC];
            [UIApplication sharedApplication].keyWindow.rootViewController = baseVC;
        
        }
    } failure:^(NSString *error) {
        
    }];
}
-(void)getData
{
    

    LLUserModel *model =  [LLUserHandle getUserData];
    [AllProjectRequest queryAllOrderByDriver:model.userid success:^(id responseObject) {
        NSArray *array = responseObject[@"resultObject"];
        self.dataArr = [HomeOrderModel arrayOfModelsFromDictionaries:array error:nil];
        NSLog(@"%@",self.dataArr);
        if (self.dataArr.count==0) {
            [self.view bringSubviewToFront:self.zhanweiView];
        }else{
            [self.view bringSubviewToFront:self.tableView];
            [self.tableView reloadData];
        }
        [self endRefresh];
    } failure:^(NSError *error) {
         [self.view bringSubviewToFront:self.zhanweiView];
        _zhanweiTitle.text = @"加载失败";
        [self endRefresh];
    }];
 
}
- (void)endRefresh {
    if (_tableView.mj_header) {
        [_tableView.mj_header endRefreshing];
    }
    if (_tableView.mj_footer) {
        [_tableView.mj_footer endRefreshing];
    }
}

-(void)setNavigation
{
    self.navigationItem.rightBarButtonItem  = [[UIBarButtonItem alloc] initWithCustomView:[self barButtonItemWithImage:[UIImage imageNamed:@"user"] highImage:[UIImage imageNamed:@"userthree"] target:self action:@selector(jumpToPersonVC)]];

}
-(UIView*)barButtonItemWithImage:(UIImage *)image highImage:(UIImage *)highImage target:(id)target action:(SEL)action{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setBackgroundImage:image forState:UIControlStateNormal];
    [btn setBackgroundImage:highImage forState:UIControlStateHighlighted];
    [btn sizeToFit];
    btn.size = CGSizeMake(16, 20);
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    UIView *containView = [[UIView alloc] initWithFrame:btn.bounds];
    [containView addSubview:btn];
    return containView;
}
//跳转到个人中心
-(void)jumpToPersonVC
{
    LLPersonMianViewController  * personMianViewController = [LLPersonMianViewController new];
    [self.navigationController pushViewController:personMianViewController animated:YES];
}

#pragma mark collection delegate dataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kScaleNum(80);
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kHomeCellIdentifier forIndexPath:indexPath];
    cell.model = self.dataArr[indexPath.row];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SendDetailViewController *sendDetail = [[SendDetailViewController alloc]init];
    HomeOrderModel *model = self.dataArr[indexPath.row];
    sendDetail.orderId = model.order_id;
    sendDetail.start = model.route_s;
    sendDetail.end = model.route_e;
    [self.navigationController pushViewController:sendDetail animated:YES];

}
-(UIView *)zhanweiView{
    if (!_zhanweiView) {
        _zhanweiView = [UIView new];
        [self.view addSubview:_zhanweiView];
        
        UIImageView * imageView = [UIImageView new];
        imageView.image = [UIImage imageNamed:@"empty"];
        [_zhanweiView addSubview:imageView];
        
        _zhanweiTitle = [UILabel new];
        _zhanweiTitle.text = @"暂无数据";
        _zhanweiTitle.textColor = [UIColor lightGrayColor];
        [_zhanweiView addSubview:_zhanweiTitle];

        [_zhanweiView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.bottom.mas_equalTo(0);
        }];
        
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(0);
            make.top.mas_equalTo(kScreenFrameH/5);
            make.width.height.equalTo(@70);
        }];
        
        [_zhanweiTitle mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(imageView.mas_bottom).offset(15);
            make.centerX.mas_equalTo(0);
        }];
    }
    return _zhanweiView;
}
@end
